package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQueryBizAppSceneReq;
import com.sg.dto.base.framework.req.QueryBizAppSceneDetailReq;
import com.sg.dto.base.framework.req.QueryBizAppSceneListReq;
import com.sg.entity.BizAppScene;
import com.sg.service.base.framework.MBizAppSceneService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.BizAppSceneMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MBizAppSceneServiceImpl
        extends ServiceImpl<BizAppSceneMapper, BizAppScene>
        implements MBizAppSceneService {

    @Autowired
    BizAppSceneMapper bizAppSceneMapper;

    /**
     * 1-1-07批量查询业务应用列表[1216]
     * gen by moon at 9/1/2022, 7:18:28 PM
     */
    @Trace(operationName = "1-1-07批量查询业务应用列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<BizAppScene> batchQueryBizAppScene(BatchQueryBizAppSceneReq reqDto) {

        try {
            QueryWrapper<BizAppScene> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getBizAppSceneList() == null || reqDto.getBizAppSceneList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("biz_app_id", reqDto.getBizAppSceneList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(BizAppScene.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(BizAppScene.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(BizAppScene.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<BizAppScene>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-07查询业务应用列表[223]
     * gen by moon at 9/1/2022, 7:18:27 PM
     */
    @Trace(operationName = "1-1-07查询业务应用列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<BizAppScene> queryBizAppSceneList(QueryBizAppSceneListReq reqDto) {

        try {
            QueryWrapper<BizAppScene> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(BizAppScene.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(BizAppScene.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(BizAppScene.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<BizAppScene>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-07查询业务应用详情[32]
     * gen by moon at 9/1/2022, 7:18:26 PM
     */
    @Trace(operationName = "1-1-07查询业务应用详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BizAppScene queryBizAppSceneDetail(QueryBizAppSceneDetailReq reqDto) {

        try {

            QueryWrapper<BizAppScene> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<BizAppScene> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                BizAppScene retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-07删除业务应用	[31]
     * gen by moon at 9/1/2022, 7:18:26 PM
     */
    @Trace(operationName = "1-1-07删除业务应用	")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBizAppScene(String bizAppId) {

        return super.removeById(bizAppId);
    }

    /**
     * 1-1-07修改业务应用	[30]
     * gen by moon at 9/1/2022, 7:18:25 PM
     */
    @Trace(operationName = "1-1-07修改业务应用	")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBizAppScene(BizAppScene bizAppScene) {
        bizAppScene.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (bizAppScene != null && bizAppScene.getSubjectLifeCycle() != null && bizAppScene.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            bizAppScene.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (bizAppScene != null && bizAppScene.getSubjectLifeCycle() != null && bizAppScene.getSubjectLifeCycle().equals("ARCHIVING")) {
            bizAppScene.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        bizAppScene.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (bizAppScene != null && bizAppScene.getSubjectLifeCycle() != null && bizAppScene.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            bizAppScene.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (bizAppScene != null && bizAppScene.getSubjectLifeCycle() != null && bizAppScene.getSubjectLifeCycle().equals("ARCHIVING")) {
            bizAppScene.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(bizAppScene);
    }

    /**
     * 1-1-07新增业务应用[29]
     * gen by moon at 9/1/2022, 7:18:25 PM
     */
    @Trace(operationName = "1-1-07新增业务应用")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addBizAppScene(BizAppScene bizAppScene) {
        bizAppScene.setBizAppId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        bizAppScene.setIsArchive("FALSE");
        bizAppScene.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        bizAppScene.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (bizAppScene != null && bizAppScene.getSubjectLifeCycle() != null && bizAppScene.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            bizAppScene.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        bizAppScene.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        bizAppScene.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (bizAppScene != null && bizAppScene.getSubjectLifeCycle() != null && bizAppScene.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            bizAppScene.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        bizAppScene.setIsValid("FALSE");
        bizAppScene.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        bizAppScene.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(bizAppScene)) {
            return bizAppScene.getBizAppId();
        } else {
            return "-1";
        }

    }
}
